(function() {

this.codium = this.codium || {};
this.codium.panel = this.codium.panel || {};

this.codium.panel.File = codium.panel.BasicPanel.extend(
{
	init : function() 
	{
		this._super();
		
		this.setIcon(this.libDir + "/img/icon/menuFile.png");
		this.setCaption("Project File");
		
		this.service = new codium.CodiumService();
		
		this.pnlFile = new pasta.webgui.ContainerControl();
		this.pnlFile.setLeft(10);
		this.pnlFile.setTop(40);
		this.pnlFile.setWidth(this.getWidth() - (2 * this.pnlFile.getLeft()));
		this.pnlFile.setHeight(this.getHeight() - this.pnlFile.getTop() - this.pnlFile.getLeft());
		this.pnlFile.setBorderColor("#FFFFFF");
		this.pnlFile.setBgColor("#FEFEFE");
		this.pnlFile.setShadow("0px 2px 4px #000000");
		this.addControl(this.pnlFile);
		
			this.lblFileCaption = new pasta.webgui.Label();
			this.lblFileCaption.setLeft(5);
			this.lblFileCaption.setTop(5);
			this.lblFileCaption.setWidth(200);
			this.lblFileCaption.setCaption("Project File :");
			this.lblFileCaption.font.setBold(true);
			this.lblFileCaption.font.setSize(12);
			this.pnlFile.addControl(this.lblFileCaption);
			
			this.shpFileCaption = new pasta.webgui.Shape();
			this.shpFileCaption.setLeft(this.lblFileCaption.getLeft());
			this.shpFileCaption.setTop(this.lblFileCaption.getTop() + 20);
			this.shpFileCaption.setWidth(this.pnlFile.getWidth() - (2 * this.shpFileCaption.getLeft()));
			this.shpFileCaption.setMode(2);
			this.pnlFile.addControl(this.shpFileCaption);
			
			this.listFile = new codium.control.FileList();
			this.listFile.setLeft(this.shpFileCaption.getLeft());
			this.listFile.setTop(this.shpFileCaption.getTop() + 10);
			this.listFile.setWidth(this.shpFileCaption.getWidth());
			this.listFile.setHeight(this.pnlFile.getHeight() - this.listFile.getTop() - 50);
			this.pnlFile.addControl(this.listFile);
			
			this.btnNewFile = new pasta.webgui.Button();
			this.btnNewFile.setLeft(this.listFile.getLeft());
			this.btnNewFile.setTop(this.listFile.getTop() + this.listFile.getHeight() + 5);
			this.btnNewFile.setCaption("New File");
			this.btnNewFile.setWidth(95);
			this.btnNewFile.getOnClick().setHandler(this, this.btnNewFileClick);
			this.pnlFile.addControl(this.btnNewFile);
			
			this.btnRefreshFile = new pasta.webgui.Button();
			this.btnRefreshFile.setLeft(this.btnNewFile.getLeft() + this.btnNewFile.getWidth() + 3);
			this.btnRefreshFile.setTop(this.btnNewFile.getTop());
			this.btnRefreshFile.setCaption("Refresh");
			this.btnRefreshFile.setWidth(80);
			this.btnRefreshFile.getOnClick().setHandler(this, this.btnRefreshFileClick);
			this.pnlFile.addControl(this.btnRefreshFile);
	},	
	
	initPanel : function()
	{
		this.refresh();
	},
	
	refresh : function()
	{
		var target = this;
		this.service.listFile(app.projectId, 
								function(result)
								{
									target.updateList(result);
								});
	},
	
	updateList : function(list)
	{
		this.listFile.clear();
		
		for (var i = 0; i < list.length; i++)
		{
			var obj = list[i];
			
			this.listFile.addFile(obj.id, obj.fileName, obj.uploadUserName, obj.uploadDate, obj.description);
		}
	},
	
	btnNewFileClick : function(sender)
	{
		if (this.pnlNewFile == null)
		{
			this.pnlNewFile = new codium.panel.NewFile();
			this.pnlNewFile.getOnOK().setHandler(this, this.pnlNewFileOK);
			this.pnlNewFile.setWidth(stage.getWidth());
			this.pnlNewFile.setHeight(stage.getHeight());
			stage.addControl(this.pnlNewFile);
		}
		
		this.pnlNewFile.show("file", -1);
	},
	
	pnlNewFileOK : function(sender)
	{
		this.refresh();
	},
	
	//-------------------- Service Event Handler --------------
	
	setWidth : function(data)
	{
		this._super(data);
		
		if (this.pnlFile != null)
		{
			this.pnlFile.setWidth(this.getWidth() - (2 * this.pnlFile.getLeft()));
			
				this.shpFileCaption.setWidth(this.pnlFile.getWidth() - (2 * this.shpFileCaption.getLeft()));
				this.listFile.setWidth(this.shpFileCaption.getWidth());
		}
	},
	
	setHeight : function(data)
	{
		this._super(data);
		
		if (this.pnlFile != null)
		{
			this.pnlFile.setHeight(this.getHeight() - this.pnlFile.getTop() - this.pnlFile.getLeft());
			this.listFile.setHeight(this.pnlFile.getHeight() - this.listFile.getTop() - 50);
			
			this.btnNewFile.setTop(this.listFile.getTop() + this.listFile.getHeight() + 5);
			this.btnRefreshFile.setTop(this.btnNewFile.getTop());
		}
	},
	
});

})();